import string

class Instructions:
    def __init__(self):
        self.instrs = {
            'add16'   :  [3, ['w', 'w', 'w'], self.add16],
            'add32'   :  [3, ['v', 'v', 'v'], self.add32],
            'add64'   :  [3, ['u', 'u', 'u'], self.add64],
            'addc16'  :  [3, ['w', 'w', 'w'], self.addc16],
            'addc32'  :  [3, ['v', 'v', 'v'], self.addc32],
            'addc64'  :  [3, ['u', 'u', 'u'], self.addc64],
            'add16d'  :  [2, ['w', 'w'], self.add16d],
            'add32d'  :  [2, ['v', 'v'], self.add32d],
            'add64d'  :  [2, ['u', 'u'], self.add64d],
            'addc16d' :  [2, ['w', 'w'], self.addc16d],
            'addc32d' :  [2, ['v', 'v'], self.addc32d],
            'addc64d' :  [2, ['u', 'u'], self.addc64d],            
            'sub16'   :  [3, ['w', 'w', 'w'], self.sub16],
            'sub32'   :  [3, ['v', 'v', 'v'], self.sub32],
            'sub64'   :  [3, ['u', 'u', 'u'], self.sub64],
            'subb16'  :  [3, ['w', 'w', 'w'], self.subb16],
            'subb32'  :  [3, ['v', 'v', 'v'], self.subb32],
            'subb64'  :  [3, ['u', 'u', 'u'], self.subb64],
            'sub16d'  :  [2, ['w', 'w'], self.sub16d],
            'sub32d'  :  [2, ['v', 'v'], self.sub32d],
            'sub64d'  :  [2, ['u', 'u'], self.sub64d],
            'subb16d' :  [2, ['w', 'w'], self.subb16d],
            'subb32d' :  [2, ['v', 'v'], self.subb32d],
            'subb64d' :  [2, ['u', 'u'], self.subb64d],
            'and32'   :  [3, ['w', 'w', 'w'], self.and16],
            'and32'   :  [3, ['v', 'v', 'v'], self.and32],
            'and64'   :  [3, ['u', 'u', 'u'], self.and64],
            'or16'  :  [3, ['w', 'w', 'w'], self.or16],
            'or32'  :  [3, ['v', 'v', 'v'], self.or32],
            'or64'  :  [3, ['u', 'u', 'u'], self.or64],
            'and16d'  :  [2, ['w', 'w'], self.and16d],
            'and32d'  :  [2, ['v', 'v'], self.and32d],
            'and64d'  :  [2, ['u', 'u'], self.and64d],
            'or16d'  :  [2, ['w', 'w'], self.or16d],
            'or32d'  :  [2, ['v', 'v'], self.or32d],
            'or64d'  :  [2, ['u', 'u'], self.or64d],
            'xor16'  :  [3, ['w', 'w', 'w'], self.xor16],
            'xor32'  :  [3, ['v', 'v', 'v'], self.xor32],
            'xor64'  :  [3, ['u', 'u', 'u'], self.xor64],       
            'xor16d'  :  [2, ['w', 'w'], self.xor16d],
            'xor32d'  :  [2, ['v', 'v'], self.xor32d],
            'xor64d'  :  [2, ['u', 'u'], self.xor64d],           
            'not16'  :  [2, ['w', 'w'], self.not16],
            'not32'  :  [2, ['v', 'v'], self.not32],
            'not64'  :  [2, ['u', 'u'], self.not64],
            'not16d'  :  [1, ['w'], self.not16d],
            'not32d'  :  [1, ['v'], self.not32d],
            'not64d'  :  [1, ['u'], self.not64d],
            'ld8'   : [2,['w','i'],self.ld8],
            'ld16'   : [2,['w','i'],self.ld16],
            'ld32'   : [2,['w','i'],self.ld32],
            'ld64'   : [2,['w','i'],self.ld64],
            'st8'   : [2,['w','i'],self.st8],
            'st16'   : [2,['w','i'],self.st16],
            'st32'   : [2,['w','i'],self.st32],
            'st64'   : [2,['w','i'],self.st64],
            'ldi4'   : [2,['w','i'],self.ldi4],
            'ldi8'   : [2,['w','i'],self.ldi8],
            'ldi16'   : [2,['w','i'],self.ldi16],
            'ldi32'   : [2,['w','i'],self.ldi32],
            'ldi64'   : [2,['w','i'],self.ldi64],
#            'call'    :  [1, ['u'], self.call]
        }
    
    def add16(self, x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr16(regs[1])+ self.grs.read_gr16(regs[2])
        self.grs.write_gr16(tmp & 0xFFFF, regs[0])
        self.srs.setCCR_N(tmp & 0x8000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFF == 0)
        self.srs.setCCR_V(0)
        if (self.grs.read_gr16(regs[1]) ^ self.grs.read_gr16(regs[2]) & 0x8000) :
            self.srs.setCCR_V((self.grs.read_gr16(regs[1])^tmp&0x8000)>>15)   
        self.srs.setCCR_C(tmp & 0x10000 != 0)
          
    def add32(self, x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr32(regs[1]) + self.grs.read_gr32(regs[2])
        self.grs.write_gr32(tmp & 0xFFFFFFFF, regs[0])
        self.srs.setCCR_N(tmp & 0x80000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffff == 0)
        self.srs.setCCR_V(0)
        if (self.grs.read_gr32(regs[1]) ^ self.grs.read_gr32(regs[2]) & 0x80000000) :
            self.srs.setCCR_V((self.grs.read_gr32(regs[1])^tmp&0x80000000)>>31)   
        self.srs.setCCR_C(tmp & 0x100000000 != 0)

    def add64(self, x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr64(regs[1]) + self.grs.read_gr64(regs[2])
        self.grs.write_gr64(tmp & 0xFFFFFFFFFFFFFFFF, regs[0])
        self.srs.setCCR_N(tmp & 0x8000000000000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffffffffffff == 0)
        self.srs.setCCR_V(0)
        if (self.grs.read_gr64(regs[1]) ^ self.grs.read_gr64(regs[2]) & 0x8000000000000000) :
            self.srs.setCCR_V((self.grs.read_gr64(regs[1])^tmp&0x8000000000000000)>>63)   
        self.srs.setCCR_C(tmp & 0x10000000000000000 != 0)

    def addc16(self, x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr16(regs[1]) + self.grs.read_gr16(regs[2])
        self.grs.write_gr16(tmp & 0xFFFF, regs[0])
        self.srs.setCCR_N(tmp & 0x8000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFF == 0)
        self.srs.setCCR_V(0)
        if (self.grs.read_gr16(regs[1]) ^ self.grs.read_gr16(regs[2]) & 0x8000) :
            self.srs.setCCR_V((self.grs.read_gr16(regs[1])^tmp&0x8000)>>15)   
        self.srs.setCCR_C(tmp & 0x10000 != 0)
        
    def addc32(self, x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr32(regs[1]) + self.grs.read_gr32(regs[2])
        self.grs.write_gr32(tmp & 0xFFFFFFFF, regs[0])
        self.srs.setCCR_N(tmp & 0x80000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffff == 0)
        self.srs.setCCR_V(0)
        if (self.grs.read_gr32(regs[1]) ^ self.grs.read_gr32(regs[2]) & 0x80000000) :
            self.srs.setCCR_V((self.grs.read_gr32(regs[1])^tmp&0x80000000)>>31)   
        self.srs.setCCR_C(tmp & 0x100000000 != 0)

    def addc64(self, x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr64(regs[1]) + self.grs.read_gr64(regs[2])
        self.grs.write_gr64(tmp & 0xFFFFFFFFFFFFFFFF, regs[0])
        self.srs.setCCR_N(tmp & 0x8000000000000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffffffffffff == 0)
        self.srs.setCCR_V(0)
        if (self.grs.read_gr64(regs[1]) ^ self.grs.read_gr64(regs[2]) & 0x8000000000000000) :
            self.srs.setCCR_V((self.grs.read_gr64(regs[1])^tmp&0x8000000000000000)>>63)   
        self.srs.setCCR_C(tmp & 0x10000000000000000 != 0)
 
        
        
        
        
        
    def add16d(self, x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr16(regs[0]) + self.grs.read_gr16(regs[1])
        self.srs.setCCR_N(tmp & 0x8000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFF == 0)
        self.srs.setCCR_V(0)
        if (self.grs.read_gr16(regs[0]) ^ self.grs.read_gr16(regs[1]) & 0x8000) :
            self.srs.setCCR_V((self.grs.read_gr16(regs[1])^tmp&0x8000)>>15)   
        self.srs.setCCR_C(tmp & 0x10000 != 0)
        self.grs.write_gr16(tmp & 0xFFFF, regs[0])
        
    def add32d(self, x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr32(regs[0]) + self.grs.read_gr32(regs[1])
        self.srs.setCCR_N(tmp & 0x80000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffff == 0)
        self.srs.setCCR_V(0)
        if (self.grs.read_gr32(regs[1]) ^ self.grs.read_gr32(regs[0]) & 0x80000000) :
            self.srs.setCCR_V((self.grs.read_gr32(regs[1])^tmp&0x80000000)>>31)   
        self.srs.setCCR_C(tmp & 0x100000000 != 0)
        self.grs.write_gr32(tmp & 0xFFFFFFFF, regs[0])
        
    def add64d(self, x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr64(regs[0]) + self.grs.read_gr64(regs[1])
        self.srs.setCCR_N(tmp & 0x8000000000000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffffffffffff == 0)
        self.srs.setCCR_V(0)
        if (self.grs.read_gr64(regs[1]) ^ self.grs.read_gr64(regs[0]) & 0x8000000000000000) :
            self.srs.setCCR_V((self.grs.read_gr64(regs[1])^tmp&0x8000000000000000)>>63)   
        self.srs.setCCR_C(tmp & 0x10000000000000000 != 0)
        self.grs.write_gr64(tmp & 0xFFFFFFFFFFFFFFFF, regs[0])
              
    def addc16d(self, x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr16(regs[0]) + self.grs.read_gr16(regs[1])
        self.srs.setCCR_N(tmp & 0x8000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFF == 0)
        self.srs.setCCR_V(0)
        if (self.grs.read_gr16(regs[0]) ^ self.grs.read_gr16(regs[1]) & 0x8000) :
            self.srs.setCCR_V((self.grs.read_gr16(regs[1])^tmp&0x8000)>>15)   
        self.srs.setCCR_C(tmp & 0x10000 != 0)
        self.grs.write_gr16(tmp & 0xFFFF, regs[0])
        
    def addc32d(self, x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr32(regs[0]) + self.grs.read_gr32(regs[1])
        self.srs.setCCR_N(tmp & 0x80000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffff == 0)
        self.srs.setCCR_V(0)
        if (self.grs.read_gr32(regs[1]) ^ self.grs.read_gr32(regs[0]) & 0x80000000) :
            self.srs.setCCR_V((self.grs.read_gr32(regs[1])^tmp&0x80000000)>>31)   
        self.srs.setCCR_C(tmp & 0x100000000 != 0)
        self.grs.write_gr32(tmp & 0xFFFFFFFF, regs[0])
        
    def addc64d(self, x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr64(regs[0]) + self.grs.read_gr64(regs[1])
        self.srs.setCCR_N(tmp & 0x8000000000000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffffffffffff == 0)
        self.srs.setCCR_V(0)
        if (self.grs.read_gr64(regs[1]) ^ self.grs.read_gr64(regs[0]) & 0x8000000000000000) :
            self.srs.setCCR_V((self.grs.read_gr64(regs[1])^tmp&0x8000000000000000)>>63)   
        self.srs.setCCR_C(tmp & 0x10000000000000000 != 0)
        self.grs.write_gr64(tmp & 0xFFFFFFFFFFFFFFFF, regs[0])
        
    def sub16(self, x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr16(regs[1]) - self.grs.read_gr16(regs[2])
        self.srs.setCCR_N(tmp & 0x8000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFF == 0)
        self.srs.setCCR_V((self.grs.read_gr16(regs[1])^tmp&0x8000)>>15)
        if (self.grs.read_gr16(regs[1]) ^ self.grs.read_gr16(regs[2]) & 0x8000) :
            self.srs.setCCR_V(0)
        self.srs.setCCR_C(tmp & 0x10000 != 0)
        self.grs.write_gr16(tmp & 0xFFFF, regs[0])   
        
    def sub32(self, x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr32(regs[1]) - self.grs.read_gr32(regs[2])
        self.srs.setCCR_N(tmp & 0x80000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffff == 0)
        self.srs.setCCR_V((self.grs.read_gr32(regs[1])^tmp&0x80000000)>>31)
        if (self.grs.read_gr32(regs[1]) ^ self.grs.read_gr32(regs[2]) & 0x80000000) :
            self.srs.setCCR_V(0)   
        self.srs.setCCR_C(tmp & 0x100000000 != 0)
        self.grs.write_gr32(tmp & 0xFFFFFFFF, regs[0])        
        
    def sub64(self, x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr64(regs[1]) - self.grs.read_gr64(regs[2])
        self.srs.setCCR_N(tmp & 0x8000000000000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffffffffffff == 0)
        self.srs.setCCR_V((self.grs.read_gr64(regs[1])^tmp&0x8000000000000000)>>63)
        if (self.grs.read_gr64(regs[1]) ^ self.grs.read_gr64(regs[2]) & 0x8000000000000000) :
            self.srs.setCCR_V(0)   
        self.srs.setCCR_C(tmp & 0x10000000000000000 != 0)
        self.grs.write_gr64(tmp & 0xFFFFFFFFFFFFFFFF, regs[0])

    def subb16(self, x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr16(regs[1]) - self.grs.read_gr16(regs[2])
        self.srs.setCCR_N(tmp & 0x8000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFF == 0)
        self.srs.setCCR_V((self.grs.read_gr16(regs[1])^tmp&0x8000)>>15)
        if (self.grs.read_gr16(regs[1]) ^ self.grs.read_gr16(regs[2]) & 0x8000) :
            self.srs.setCCR_V(0)
        self.srs.setCCR_C(tmp & 0x10000 != 0)
        self.grs.write_gr16(tmp & 0xFFFF, regs[0])   
        
    def subb32(self, x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr32(regs[1]) - self.grs.read_gr32(regs[2])
        self.srs.setCCR_N(tmp & 0x80000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffff == 0)
        self.srs.setCCR_V((self.grs.read_gr32(regs[1])^tmp&0x80000000)>>31)
        if (self.grs.read_gr32(regs[1]) ^ self.grs.read_gr32(regs[2]) & 0x80000000) :
            self.srs.setCCR_V(0)   
        self.srs.setCCR_C(tmp & 0x100000000 != 0)
        self.grs.write_gr32(tmp & 0xFFFFFFFF, regs[0])        
        
    def subb64(self, x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr64(regs[1]) - self.grs.read_gr64(regs[2])
        self.srs.setCCR_N(tmp & 0x8000000000000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffffffffffff == 0)
        self.srs.setCCR_V((self.grs.read_gr64(regs[1])^tmp&0x8000000000000000)>>63)
        if (self.grs.read_gr64(regs[1]) ^ self.grs.read_gr64(regs[2]) & 0x8000000000000000) :
            self.srs.setCCR_V(0)   
        self.srs.setCCR_C(tmp & 0x10000000000000000 != 0)
        self.grs.write_gr64(tmp & 0xFFFFFFFFFFFFFFFF, regs[0])
 
        
        
        
        
        

    def sub16d(self, x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr16(regs[0]) - self.grs.read_gr16(regs[1])
        self.srs.setCCR_N(tmp & 0x8000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFF == 0)
        self.srs.setCCR_V((self.grs.read_gr16(regs[0])^tmp&0x8000)>>15)
        if (self.grs.read_gr16(regs[1]) ^ self.grs.read_gr16(regs[0]) & 0x8000) :
            self.srs.setCCR_V(0)
        self.srs.setCCR_C(tmp & 0x10000 != 0)
        self.grs.write_gr16(tmp & 0xFFFF, regs[0])   
        
    def sub32d(self, x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr32(regs[0]) - self.grs.read_gr32(regs[1])
        self.srs.setCCR_N(tmp & 0x80000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffff == 0)
        self.srs.setCCR_V((self.grs.read_gr32(regs[0])^tmp&0x80000000)>>31)
        if (self.grs.read_gr32(regs[1]) ^ self.grs.read_gr32(regs[0]) & 0x80000000) :
            self.srs.setCCR_V(0)   
        self.srs.setCCR_C(tmp & 0x100000000 != 0)
        self.grs.write_gr32(tmp & 0xFFFFFFFF, regs[0])        
        
    def sub64d(self, x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr64(regs[0]) - self.grs.read_gr64(regs[1])
        self.srs.setCCR_N(tmp & 0x8000000000000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffffffffffff == 0)
        self.srs.setCCR_V((self.grs.read_gr64(regs[0])^tmp&0x8000000000000000)>>63) 
        if (self.grs.read_gr64(regs[1]) ^ self.grs.read_gr64(regs[0]) & 0x8000000000000000) :
            self.srs.setCCR_V(0)   
        self.srs.setCCR_C(tmp & 0x10000000000000000 != 0)
        self.grs.write_gr64(tmp & 0xFFFFFFFFFFFFFFFF, regs[0])    
        
    def subb16d(self, x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr16(regs[0]) - self.grs.read_gr16(regs[1])
        self.srs.setCCR_N(tmp & 0x8000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFF == 0)
        self.srs.setCCR_V((self.grs.read_gr16(regs[0])^tmp&0x8000)>>15)
        if (self.grs.read_gr16(regs[1]) ^ self.grs.read_gr16(regs[0]) & 0x8000) :
            self.srs.setCCR_V(0)
        self.srs.setCCR_C(tmp & 0x10000 != 0)
        self.grs.write_gr16(tmp & 0xFFFF, regs[0])   
        
    def subb32d(self, x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr32(regs[0]) - self.grs.read_gr32(regs[1])
        self.srs.setCCR_N(tmp & 0x80000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffff == 0)
        self.srs.setCCR_V((self.grs.read_gr32(regs[0])^tmp&0x80000000)>>31)
        if (self.grs.read_gr32(regs[1]) ^ self.grs.read_gr32(regs[0]) & 0x80000000) :
            self.srs.setCCR_V(0)   
        self.srs.setCCR_C(tmp & 0x100000000 != 0)
        self.grs.write_gr32(tmp & 0xFFFFFFFF, regs[0])        
        
    def subb64d(self, x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr64(regs[0]) - self.grs.read_gr64(regs[1])
        self.srs.setCCR_N(tmp & 0x8000000000000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffffffffffff == 0)
        self.srs.setCCR_V((self.grs.read_gr64(regs[0])^tmp&0x8000000000000000)>>63)
        if (self.grs.read_gr64(regs[1]) ^ self.grs.read_gr64(regs[0]) & 0x8000000000000000) :
            self.srs.setCCR_V(0)   
        self.srs.setCCR_C(tmp & 0x10000000000000000 != 0)
        self.grs.write_gr64(tmp & 0xFFFFFFFFFFFFFFFF, regs[0])        
    
    def and64(self, x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr64(regs[1]) & self.grs.read_gr64(regs[2])
        self.srs.setCCR_N(tmp & 0x8000000000000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffffffffffff == 0)
        self.grs.write_gr64(tmp & 0xFFFFFFFFFFFFFFFF, regs[0]) 
                
    def and64d(self, x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr64(regs[0]) & self.grs.read_gr64(regs[1])
        self.srs.setCCR_N(tmp & 0x8000000000000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffffffffffff == 0)
        self.grs.write_gr64(tmp & 0xFFFFFFFFFFFFFFFF, regs[0])               
        
    def and32(self, x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr32(regs[1]) & self.grs.read_gr32(regs[2])
        self.srs.setCCR_N(tmp & 0x80000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffff == 0)
        self.grs.write_gr32(tmp & 0xFFFFFFFF, regs[0]) 
                
    def and32d(self, x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr32(regs[0]) & self.grs.read_gr32(regs[1])
        self.srs.setCCR_N(tmp & 0x80000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffff == 0)
        self.grs.write_gr32(tmp & 0xFFFFFFFF, regs[0])           
 
    def and16(self, x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr16(regs[1]) & self.grs.read_gr16(regs[2])
        self.srs.setCCR_N(tmp & 0x8000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFF == 0)
        self.grs.write_gr32(tmp & 0xFFFF, regs[0]) 
                
    def and16d(self, x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr16(regs[0]) & self.grs.read_gr16(regs[1])
        self.srs.setCCR_N(tmp & 0x8000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFF == 0)
        self.grs.write_gr16(tmp & 0xFFFF, regs[0])  
        
        
        
        
        
        
        
        
        
    def or64(self, x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr64(regs[1]) | self.grs.read_gr64(regs[2])
        self.srs.setCCR_N(tmp & 0x8000000000000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffffffffffff == 0)
        self.grs.write_gr64(tmp & 0xFFFFFFFFFFFFFFFF, regs[0]) 
                
    def or64d(self, x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr64(regs[0]) | self.grs.read_gr64(regs[1])
        self.srs.setCCR_N(tmp & 0x8000000000000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffffffffffff == 0)
        self.grs.write_gr64(tmp & 0xFFFFFFFFFFFFFFFF, regs[0])               
        
    def or32(self, x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr32(regs[1]) | self.grs.read_gr32(regs[2])
        self.srs.setCCR_N(tmp & 0x80000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffff == 0)
        self.grs.write_gr32(tmp & 0xFFFFFFFF, regs[0]) 
                
    def or32d(self, x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr32(regs[0]) | self.grs.read_gr32(regs[1])
        self.srs.setCCR_N(tmp & 0x80000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffff == 0)
        self.grs.write_gr32(tmp & 0xFFFFFFFF, regs[0])           
 
    def or16(self, x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr16(regs[1]) | self.grs.read_gr16(regs[2])
        self.srs.setCCR_N(tmp & 0x8000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFF == 0)
        self.grs.write_gr32(tmp & 0xFFFF, regs[0]) 
                
    def or16d(self, x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr16(regs[0]) | self.grs.read_gr16(regs[1])
        self.srs.setCCR_N(tmp & 0x8000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFF == 0)
        self.grs.write_gr16(tmp & 0xFFFF, regs[0])          
        
        
        
    
        
        
        
    def xor64(self, x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr64(regs[1]) ^ self.grs.read_gr64(regs[2])
        self.srs.setCCR_N(tmp & 0x8000000000000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffffffffffff == 0)
        self.grs.write_gr64(tmp & 0xFFFFFFFFFFFFFFFF, regs[0]) 
                
    def xor64d(self, x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr64(regs[0]) ^ self.grs.read_gr64(regs[1])
        self.srs.setCCR_N(tmp & 0x8000000000000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffffffffffff == 0)
        self.grs.write_gr64(tmp & 0xFFFFFFFFFFFFFFFF, regs[0])               
        
    def xor32(self, x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr32(regs[1]) ^ self.grs.read_gr32(regs[2])
        self.srs.setCCR_N(tmp & 0x80000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffff == 0)
        self.grs.write_gr32(tmp & 0xFFFFFFFF, regs[0]) 
                
    def xor32d(self, x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr32(regs[0]) ^ self.grs.read_gr32(regs[1])
        self.srs.setCCR_N(tmp & 0x80000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffff == 0)
        self.grs.write_gr32(tmp & 0xFFFFFFFF, regs[0])           
 
    def xor16(self, x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr16(regs[1]) ^ self.grs.read_gr16(regs[2])
        self.srs.setCCR_N(tmp & 0x8000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFF == 0)
        self.grs.write_gr32(tmp & 0xFFFF, regs[0]) 
                
    def xor16d(self, x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr16(regs[0]) ^ self.grs.read_gr16(regs[1])
        self.srs.setCCR_N(tmp & 0x8000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFF == 0)
        self.grs.write_gr16(tmp & 0xFFFF, regs[0])          
        
        
    def not16(self, x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = ~self.grs.read_gr16(regs[1])
        self.srs.setCCR_N(tmp & 0x8000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFF == 0)
        self.grs.write_gr16(tmp & 0xFFFF, regs[0])      
    def not32(self, x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = ~self.grs.read_gr32(regs[1])
        self.srs.setCCR_N(tmp & 0x80000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffff == 0)
        self.grs.write_gr32(tmp & 0xFFFFFFFF, regs[0])   
    def not64(self, x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = ~self.grs.read_gr64(regs[1])
        self.srs.setCCR_N(tmp & 0x8000000000000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffffffffffff == 0)
        self.grs.write_gr64(tmp & 0xFFFFFFFFFFFFFFFF, regs[0])   
        
        
    def not16d(self, x):
        regs = []
        for i in range(1):
            regs += [self.get_regs_num(x[i])]
        tmp = ~self.grs.read_gr16(regs[0])
        self.srs.setCCR_N(tmp & 0x8000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFF == 0)
        self.grs.write_gr16(tmp & 0xFFFF, regs[0])      
    def not32d(self, x):
        regs = []
        for i in range(1):
            regs += [self.get_regs_num(x[i])]
        tmp = ~self.grs.read_gr32(regs[0])
        self.srs.setCCR_N(tmp & 0x80000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffff == 0)
        self.grs.write_gr32(tmp & 0xFFFFFFFF, regs[0])          
    def not64d(self, x):
        regs = []
        for i in range(1):
            regs += [self.get_regs_num(x[i])]
        tmp = ~self.grs.read_gr64(regs[0])
        self.srs.setCCR_N(tmp & 0x8000000000000000 != 0)
        self.srs.setCCR_Z(tmp & 0xFFFFffffffffffff == 0)
        self.grs.write_gr64(tmp & 0xFFFFFFFFFFFFFFFF, regs[0])         
        
    def sll16(self,x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr16(regs[1]) << self.grs.read_gr16(regs[2]) 
        self.grs.write_gr64(tmp & 0xFFFF, regs[0]) 
    def sll32(self,x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr32(regs[1]) << self.grs.read_gr32(regs[2]) 
        self.grs.write_gr64(tmp & 0xFFFFffff, regs[0])  
    def sll64(self,x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr64(regs[1]) << self.grs.read_gr64(regs[2]) 
        self.grs.write_gr64(tmp & 0xFFFFffffFFFFffff, regs[0])  
            
    def srl16(self,x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr16(regs[1]) >> self.grs.read_gr16(regs[2]) 
        self.grs.write_gr64(tmp & 0xFFFF, regs[0]) 
    def srl32(self,x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr32(regs[1]) >> self.grs.read_gr32(regs[2]) 
        self.grs.write_gr64(tmp & 0xFFFFffff, regs[0])  
    def srl64(self,x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr64(regs[1]) >> self.grs.read_gr64(regs[2]) 
        self.grs.write_gr64(tmp & 0xFFFFffffFFFFffff, regs[0])          
        
        
        
        
    def slld16(self,x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr16(regs[0]) << self.grs.read_gr16(regs[1]) 
        self.grs.write_gr64(tmp & 0xFFFF, regs[0]) 
    def slld32(self,x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr32(regs[0]) << self.grs.read_gr32(regs[1]) 
        self.grs.write_gr64(tmp & 0xFFFFffff, regs[0])  
    def slld64(self,x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr64(regs[0]) << self.grs.read_gr64(regs[1]) 
        self.grs.write_gr64(tmp & 0xFFFFffffFFFFffff, regs[0])  
            
    def srld16(self,x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr16(regs[0]) >> self.grs.read_gr16(regs[1]) 
        self.grs.write_gr64(tmp & 0xFFFF, regs[0]) 
    def srld32(self,x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr32(regs[0]) >> self.grs.read_gr32(regs[1]) 
        self.grs.write_gr64(tmp & 0xFFFFffff, regs[0])  
    def srld64(self,x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr64(regs[0]) >> self.grs.read_gr64(regs[1]) 
        self.grs.write_gr64(tmp & 0xFFFFffffFFFFffff, regs[0]) 
        
        
        
        

    def sla16(self,x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr16(regs[1]) << self.grs.read_gr16(regs[2]) 
        self.grs.write_gr64(tmp & 0xFFFF, regs[0]) 
    def sla32(self,x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr32(regs[1]) << self.grs.read_gr32(regs[2]) 
        self.grs.write_gr64(tmp & 0xFFFFffff, regs[0])  
    def sla64(self,x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr64(regs[1]) << self.grs.read_gr64(regs[2]) 
        self.grs.write_gr64(tmp & 0xFFFFffffFFFFffff, regs[0])  
            
    def sra16(self,x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = (self.grs.read_gr16(regs[1]) | 0xfffff0000) >> self.grs.read_gr16(regs[2]) 
        self.grs.write_gr64(tmp & 0xFFFF, regs[0]) 
    def sra32(self,x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = (self.grs.read_gr32(regs[1]) | 0xffffFFFFf00000000) >> self.grs.read_gr32(regs[2]) 
        self.grs.write_gr64(tmp & 0xFFFFffff, regs[0])  
    def sra64(self,x):
        regs = []
        for i in range(3):
            regs += [self.get_regs_num(x[i])]
        tmp = (self.grs.read_gr64(regs[1]) | 0xFFFFffffFFFFffff0000000000000000) >> self.grs.read_gr64(regs[2]) 
        self.grs.write_gr64(tmp & 0xFFFFffffFFFFffff, regs[0])          
        
        
        
        
    def slad16(self,x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr16(regs[0]) << self.grs.read_gr16(regs[1]) 
        self.grs.write_gr64(tmp & 0xFFFF, regs[0]) 
    def slad32(self,x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr32(regs[0]) << self.grs.read_gr32(regs[1]) 
        self.grs.write_gr64(tmp & 0xFFFFffff, regs[0])  
    def slad64(self,x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = self.grs.read_gr64(regs[0]) << self.grs.read_gr64(regs[1]) 
        self.grs.write_gr64(tmp & 0xFFFFffffFFFFffff, regs[0])  
            
    def srad16(self,x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = (self.grs.read_gr16(regs[1]) | 0xfffff0000) >> self.grs.read_gr16(regs[1]) 
        self.grs.write_gr64(tmp & 0xFFFF, regs[0]) 
    def srad32(self,x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = (self.grs.read_gr32(regs[1]) | 0xffffFFFFf00000000) >> self.grs.read_gr32(regs[1]) 
        self.grs.write_gr64(tmp & 0xFFFFffff, regs[0])  
    def srad64(self,x):
        regs = []
        for i in range(2):
            regs += [self.get_regs_num(x[i])]
        tmp = (self.grs.read_gr64(regs[1]) | 0xFFFFffffFFFFffff0000000000000000) >> self.grs.read_gr64(regs[1]) 
        self.grs.write_gr64(tmp & 0xFFFFffffFFFFffff, regs[0]) 
   
        
        
    def ld8(self,x):
        regs = []
        regs = [self.get_regs_num(x[0])]
        tmp = 0
        if self.dmem.has_key(x[1]):
            tmp = self.dmem[x[1]]
        self.grs.write_gr16(tmp & 0xFF,regs[0])
        
    def st8(self,x):
        regs=[]
        regs = [self.get_regs_num(x[0])]
        tmp = self.grs.read_gr16(regs[0])
        self.dmem[x[1]] = tmp & 0xff
        
        
        
    def ld16(self,x):
        regs = []
        regs = [self.get_regs_num(x[0])]
        tmp = 0
        if self.dmem.has_key(x[1]):
            tmp = self.dmem[x[1]]
        if self.dmem.has_key(x[1]+1):
            tmp += (self.dmem[x[1]+1] <<8);
        self.grs.write_gr16(tmp & 0xFFFF,regs[0])
        
    def st16(self,x):
        regs=[]
        regs = [self.get_regs_num(x[0])]
        tmp = self.grs.read_gr16(regs[0])
        self.dmem[x[1]] = tmp & 0x00ff
        self.dmem[x[1]+1] = tmp&0xff00
        
    def ld32(self,x):
        regs = []
        regs = [self.get_regs_num(x[0])]
        tmp = 0
        if self.dmem.has_key(x[1]):
            tmp = self.dmem[x[1]]
        if self.dmem.has_key(x[1]+1):
            tmp += (self.dmem[x[1]+1] <<8)
        if self.dmem.has_key(x[1]+2):
            tmp += (self.dmem[x[1]+3] <<16)
        if self.dmem.has_key(x[1]+3):
            tmp += (self.dmem[x[1]+3] <<24)
        self.grs.write_gr32(tmp & 0xFFFFffff,regs[0])
        
    def st32(self,x):
        regs=[]
        regs = [self.get_regs_num(x[0])]
        tmp = self.grs.read_gr16(regs[0])
        self.dmem[x[1]]   = tmp & 0x000000ff
        self.dmem[x[1]+1] = tmp & 0x0000ff00
        self.dmem[x[1]+2] = tmp & 0x00ff0000
        self.dmem[x[1]+3] = tmp & 0xff000000         

    def ld64(self,x):
        regs = []
        regs = [self.get_regs_num(x[0])]
        tmp = 0
        if self.dmem.has_key(x[1]):
            tmp = self.dmem[x[1]]
        if self.dmem.has_key(x[1]+1):
            tmp += (self.dmem[x[1]+1] <<8)
        if self.dmem.has_key(x[1]+2):
            tmp += (self.dmem[x[1]+3] <<16)
        if self.dmem.has_key(x[1]+3):
            tmp += (self.dmem[x[1]+3] <<24)
        if self.dmem.has_key(x[1]+4):
            tmp += (self.dmem[x[1]+4] <<32)
        if self.dmem.has_key(x[1]+5):
            tmp += (self.dmem[x[1]+5] <<40)
        if self.dmem.has_key(x[1]+6):
            tmp += (self.dmem[x[1]+6] <<48)
        if self.dmem.has_key(x[1]+7):
            tmp += (self.dmem[x[1]+7] <<56)
        self.grs.write_gr64(tmp & 0xFFFFFFFFffffffff,regs[0])
        
    def st64(self,x):
        regs=[]
        regs = [self.get_regs_num(x[0])]
        tmp = self.grs.read_gr16(regs[0])
        self.dmem[x[1]]   = tmp & 0x00000000000000ff
        self.dmem[x[1]+1] = tmp & 0x000000000000ff00
        self.dmem[x[1]+2] = tmp & 0x0000000000ff0000
        self.dmem[x[1]+3] = tmp & 0x00000000ff000000         
        self.dmem[x[1]+4] = tmp & 0x000000ff00000000
        self.dmem[x[1]+5] = tmp & 0x0000ff0000000000
        self.dmem[x[1]+6] = tmp & 0x00ff000000000000
        self.dmem[x[1]+7] = tmp & 0xff00000000000000  
        
    def ldi4(self,x):
        regs=[]
        regs = [self.get_regs_num(x[0])]
        tmp = x[1]
        self.grs.write_gr16(tmp & 0xF,regs[0])
    def ldi8(self,x):
        regs=[]
        regs = [self.get_regs_num(x[0])]
        tmp = x[1] 
        self.grs.write_gr16(tmp & 0xFF,regs[0])
    def ldi16(self,x):
        regs=[]
        regs = [self.get_regs_num(x[0])]
        tmp = x[1] 
        self.grs.write_gr16(tmp & 0xFFFF,regs[0])
    def ldi32(self,x):
        regs=[]
        regs = [self.get_regs_num(x[0])]
        tmp = x[1]
        self.grs.write_gr32(tmp & 0xFFFFFFFF,regs[0])
    def ldi64(self,x):
        regs=[]
        regs = [self.get_regs_num(x[0])]
        tmp = x[1] 
        self.grs.write_gr64(tmp & 0xFFFFFFFFffffffff,regs[0])

   # def jmp16(self,x):
    #    regs=[]
     #   regs = [self.get_regs_num(x[0])]
      #  tmp = self.imem.keys()
       # tmp = self.grs.read_gr16(regs[0])
        #self.setPC64([temp])
    
    
    def get_regs_num(self, argv):
        if argv != '':
            if argv[0] in ['w', 'u', 'v']:
                return string.atoi(argv[1:])
        return argv